package com.xyz.apis.repository;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xyz.admin.common.entity.EvUserEntity;
import com.xyz.admin.common.global.EvTicketRedisKey;
import com.xyz.apis.common.utils.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserTokenRepository {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private JedisService jedisService;

    private Map<String, EvUserEntity> userInfoMap = new HashMap<>();



    public void setToken(EvUserEntity evUserEntity){
        try {
            EvUserEntity evUserEntity1 = getNoCachUserInfo(evUserEntity.getUserid());
            if(null!=evUserEntity1){
                deleteToken(evUserEntity1.getToken(),evUserEntity.getUserid());
            }
            jedisService.hset(EvTicketRedisKey.evapi_usertoken_key,evUserEntity.getToken(), JSONObject.toJSONString(evUserEntity));
        } catch (Exception e) {
            logger.error("redis缓存用户token异常",e);
        }
    }

    public EvUserEntity getToken(String token){

        try {
            EvUserEntity evUserEntity = userInfoMap.get(token);
            if(null==evUserEntity){
                evUserEntity = JSON.parseObject(jedisService.hget(EvTicketRedisKey.evapi_usertoken_key, token), EvUserEntity.class);
                userInfoMap.put(token,evUserEntity);
            }


            return evUserEntity;
        } catch (Exception e) {
            logger.error("redis获取用户token异常",e);
        }
        return null;

    }

    public void deleteToken(String token,Long userid){
        try {
            userInfoMap.remove(token);
            jedisService.hdel(EvTicketRedisKey.evapi_usertoken_key,token);
            if(CommonUtil.moreThanZero(userid)){
                jedisService.hdel(EvTicketRedisKey.evapi_userinfo_key,String.valueOf(userid) );
            }
        } catch (Exception e) {
            logger.error("redis删除userToken异常, token: " + token,e);
        }
    }

    public void deleteTokenByAgainLogin(Long userid){
        try {
            String strs = jedisService.hget(EvTicketRedisKey.evapi_userinfo_key, String.valueOf(userid));
            if(!CommonUtil.isBlank(strs)){
                EvUserEntity userEntity = JSONObject.parseObject(strs, EvUserEntity.class);
                if(userEntity!=null){
                    jedisService.hdel(EvTicketRedisKey.evapi_userinfo_key,String.valueOf(userid) );
                    if(!CommonUtil.isBlank(userEntity.getToken())){
                        String token =userEntity.getToken();
                        userInfoMap.remove(token);
                        jedisService.hdel(EvTicketRedisKey.evapi_usertoken_key,token);
                    }
                }
            }

        } catch (Exception e) {
            logger.error("redis删除deleteTokenByAgainLogin异常, userid: " + userid,e);
        }
    }

    /**
     * 获取用户信息
     * 这里是不从userInfoMap缓存中获取的
     * 特注释勿加
     */
    public EvUserEntity getNoCachUserInfo(Long userId){
        try {
            EvUserEntity evUserEntity = JSON.parseObject(jedisService.hget(EvTicketRedisKey.evapi_userinfo_key, String.valueOf(userId)), EvUserEntity.class);
            return evUserEntity;
        } catch (Exception e) {
            logger.error("redis获取用户信息异常",e);
        }
        return null;

    }
}
